var roleId;
var win,check_tree_panel,check_tree_store;

check_tree_store = new Ext.data.TreeStore({
	proxy:  
    {  
        type: 'ajax',  
        method: 'GET',
        url: '/resource/checktree'  
    },  
    root:  
    {  
        id: "",  
        text: "选择系统资源权限",  
        leaf: false,  
        expandable: true,  
        expanded: true ,
        checked: false
    },
    sorters: [  
    {  
        property: 'leaf',  
        direction: 'ASC'  
    },  
    {  
        property: 'text',  
        direction: 'ASC'  
    }]  
})

check_tree_panel = new Ext.tree.TreePanel({
	autoWidth:true,
	autoHeight:true,
	autoScroll:true,
	animate:true,
	store: check_tree_store,
	buttons: [
       { 
    	   xtype: 'button', 
    	   text: '确  定',
    	   icon:'../../img/accept.png',
    	   handler:function(){
    		   var records = check_tree_panel.getView().getChecked(),
               //为存储ID而创建数组
               ids   = [];
	           Ext.Array.each(records, function(rec){
	               //将选中的节点的ID保存
	               ids.push(rec.get('id'));
	           }); 
	           /*if(ids.length == 0){
	        	   Ext.MessageBox.show({
	                   title: '信息提示',
	                   msg: '请选择资源权限！',
	                   buttons: Ext.Msg.OK,
	                   icon: Ext.MessageBox.INFO
	               });
	        	   return;
	           }*/
	           Ext.MessageBox.confirm('信息提示','确认授权吗？',function(val){
	       		if(val == 'yes'){
	       	    	Ext.Ajax.request({
	       	            url: '/role/addresource?resource_ids='+ids.join(',')+"&role_id="+roleId,
	       	            method : 'POST',
	       	            success: function(response, opts) {
	       	                var data = eval("("+response.responseText+")");
	       	                Ext.Msg.alert('信息提示',data.msg);
		       	            //win.hide();
		       	            //window.location.reload();
	       	            },
	       	            failure: function(response, opts) {
	       	            	var data = eval("("+response.responseText+")");
	       	            	if(!data.success){
	       	            		Ext.Msg.alert('信息提示',data.msg);
	       	            	} else {
	       	            		Ext.Msg.alert("信息提示","系统忙，请稍候再试");
	       	            	}
	       	            }
	       	        });
	       		}
	       	});
    	   }
       },
       { 
    	   xtype: 'button', 
    	   text: '关  闭',
    	   icon : '../../img/close.png',
    	   handler:function(){
    		   win.hide();
    		   //window.location.reload();
    	   }
       }
     ]
});

/*向上遍历父结点*/
var nodep=function(node){    		
	var bnode=true;
	var count=0;
	Ext.Array.each(node.childNodes,function(v){
			if(!v.data.checked){						
				bnode=false;
				count++;
				//return;			
			}		
	});
	if(!bnode && count==node.childNodes.length){
		//说明子节点全部没有选中
		return false;
	}
	return true;
};

var parentnode=function(node){
	if(node.parentNode != null){
		if(nodep(node.parentNode)){
			node.parentNode.set('checked', true);
		}else{
			node.parentNode.set('checked', false);
		}
		parentnode(node.parentNode);	
	} 
};


/*遍历子结点 选中 与取消选中操作*/
var chd=function(node,check){
	node.set('checked',check);
	if(node.isNode){
		 node.eachChild(function (child) {  
       			 chd(child,check);
		  });
	}
};

check_tree_panel.on('checkchange', function (node, checked) {
	if(checked){
		 node.eachChild(function (child) {  
   			  chd(child,true);
		 });
	}else{
		 node.eachChild(function (child) {  
   			 chd(child,false);
		 });
	}    			
	parentnode(node);  //进行父级选中操作    		
},check_tree_panel);

function authorization(role_id){
	if(!win){
		win = Ext.create('Ext.Window', {
    		title: '角色授权',
    		width: 550,
    		height: 500,
    		bodyPadding: '0 0 0',
    		'z-index': 9999999,
    		constrainHeader: true,
    		layout: 'fit',
    		closeAction:'hide',
    		items: check_tree_panel,
    		listeners:{
    			'hide':function(){
    				this.hide();
    			}
    		}
    	});
	}
	win.show();
	roleId = role_id;
	check_tree_store.load({params:{role_id:role_id}});
	//check_tree_panel默认展开所有节点
	check_tree_panel.expandAll();
}